
/****
 * Copyright (C) 2006 @autors                                            
 *                                                            ****
 * This program is free software; you can redistribute it and/or *
 * modify under the terms of the GNU General Public License as   *
 * published by the Free Software Foundation;                    *
 * either version 2 of the License.                              *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of without even the implied waranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          *
 * See the GNU General Public License for more details.          *
 *                                                               *
 * You should have received a copy of the                        *
 * GNU General Public License                                    *
 * along with this program; if not, write to the                 *
 * Free Software Foundation, Inc.,                               *
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *                                                               *
 ****                                                         ****/

#ifndef __DCORE_LIST_H__
#define __DCORE_LIST_H__

/* ##### TYPEDEF ##### */
typedef struct _d_list_s d_list_t;
typedef struct _d_list_iterator_s d_list_iterator_t;
typedef void (*d_list_free_f) (void *p_data);

/* ##### STRUCT ##### */

typedef struct _d_list_element_s {
  void *p_data;                      /*!< pointer to the data */
  struct _d_list_element_s *p_prev;  /*!< pointer to the prev element */
  struct _d_list_element_s *p_next;  /*!< pointer to the next element */
} _d_list_element_t;

struct _d_list_iterator_s {
  d_list_t *p_list;             /*!< pointer to the list */
  _d_list_element_t *p_current; /*!< pointer to the current element */
};

struct _d_list_s {
  d_list_free_f f_free;       /*!< pointer to the user free function. */
  unsigned int i_length;      /*!< length of the list */
  _d_list_element_t *p_begin; /*!< pointer to the first element */
  _d_list_element_t *p_end;   /*!< pointer to the last element */
};

/* ##### FUNCTIONS ##### */
_d_list_element_t * _d_list_new_element  (void *p_data);

d_list_iterator_t * _d_list_new_iterator (d_list_t *p_list,
						 _d_list_element_t *p_element);

/* ##### MACROS ##### */

#define d_list_push(p_list, p_data) \
		_d_list_add(p_list, (void *) p_data, d_false)

#define d_list_pushback(p_list, p_data) \
		_d_list_add(p_list, (void *) p_data, d_true)

#define d_list_value(p_iterator, p_data) \
		_d_list_value(p_iterator, (void **) p_data)

/* ##### FUNCTIONS ##### */

d_list_t * d_list_new (void);
d_list_t * d_list_new_with_free_func(d_list_free_f f_func);

void d_list_free (d_list_t **p_list); 

void d_list_set_free_func(d_list_t *p_list, d_list_free_f f_func);

void d_list_free_iterator (d_list_iterator_t **p_iterator);

d_bool_t _d_list_add (d_list_t* p_list, void* p_data, 
			     d_bool_t b_at_end);

void d_list_del (d_list_iterator_t *p_iterator);

void d_list_clear (d_list_t *p_list);

int d_list_length (d_list_t* p_list);

d_bool_t d_list_iterator_valid (d_list_iterator_t *p_iterator);

d_list_iterator_t *  d_list_at (d_list_t *p_list, unsigned int i_pos);

d_bool_t _d_list_value (d_list_iterator_t *p_iterator, 
			       void **p_data);

d_list_iterator_t * d_list_begin (d_list_t *p_list);

d_list_iterator_t * d_list_end (d_list_t *p_list);

d_bool_t d_list_prev (d_list_iterator_t **p_iterator);

d_bool_t d_list_next (d_list_iterator_t **p_iterator);

#endif /* __DCORE_LIST_H__ */

